<style></style>
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <div class="layui-form lay-header">
        <div class="toolbar">
          <div class="layui-form-item lay-query-content">
            <div class="lay-query-box">
              <div class="query-lt">
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">退换类型：</label>
                  <div class="layui-input-inline">
                    <select name="type" id="typeSearch" lay-filter="type"></select>
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">供应商名称：</label>
                  <div class="layui-input-inline">
                    <select id="supplierIdSearch" name="supplierId" lay-filter="supplierId"></select>
                  </div>
                </div>

                <div class="layui-inline">
                  <label class="layui-form-label w-auto">记录编号：</label>
                  <div class="layui-input-inline">
                    <input name="code" class="layui-input" type="text" placeholder="记录编号" />
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">订单编号：</label>
                  <div class="layui-input-inline">
                    <input name="purOrderCode" class="layui-input" type="text" placeholder="订单编号" />
                  </div>
                </div>
              </div>
              <div class="query-rt">
                <div class="layui-inline">
                  <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                  <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="lay-btn-rows">
          <div class="lay-btn-box">
            <div class="lay-btn-lt">
              <button id="contractBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
            </div>
          </div>
        </div>
      </div>
      <table class="layui-table" id="tableDiv" lay-filter="tableDiv"></table>
    </div>
  </div>
</div>
<!-- 搜查条件框-->

<!--退货状态(1.待审核 ，2已完成)-->

<script type="text/html" id="returnTableBar">
  <button class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">详情</button>
  <!-- {{# if(d.status != 2){ }} -->
  <button class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</button>
  <!-- {{# } }} {{# if(d.status == 1){ }} -->
  <button class="layui-btn  layui-btn-xs" lay-event="audit" lay-on="test-confirm">审核</button>
  <!-- {{# } }} {{# if(d.status != 2){ }} -->
  <!-- <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</button> -->
  <!-- {{# } }} -->
  <button class="layui-btn  layui-btn-xs" lay-event="sendFun">发货</button>
  <button class="layui-btn  layui-btn-xs" lay-event="sendHis">发货记录</button>
  <button class="layui-btn  layui-btn-xs" lay-event="getFun">收货</button>
  <button class="layui-btn  layui-btn-xs" lay-event="getHis">收货记录</button>
</script>
<!-- 换货状态(1.待审核 ，2待发货，3.待供应商发货 4.待收货 5.完成) -->
<script type="text/html" id="barterTableBar">
  <button class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">详情</button>
  <!-- {{# if(d.status == 1){ }} -->
  <button class="layui-btn layui-btn-warm  layui-btn-xs" lay-event="edit">修改</button>
  <!-- {{# } }} {{# if(d.status == 2){ }} -->
  <button class="layui-btn  layui-btn-xs" lay-event="sendFun">发货</button>
  <!-- {{# } }} {{# if(d.status == 3){ }}  -->
  <button class="layui-btn  layui-btn-xs" lay-event="getFun">收货</button>
  <!-- {{# } }} {{# if(d.status == 1){ }} -->
  <button class="layui-btn  layui-btn-xs" lay-event="auditUrl" lay-on="test-confirm">审核</button>
  <!-- {{# } }} -->

  <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</button>
</script>
<script>
  layui.use(['layer', 'form', 'table', 'admin', 'upload', 'tree', 'laytpl', 'laydate'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var config = layui.config;
    var admin = layui.admin;
    var upload = layui.upload;
    var laytpl = layui.laytpl;
    var laydate = layui.laydate;
    var syncProductName = '';
    var syncProductId = 0;
    let typeObj = {};
    $(function () {
      if ($('#exchangeRecords').length) {
        typeObj = {
          typeNameData: '换货',
          selectTypeSrc: 'enterprise/purchase/admin/barterGoodsType/list',
          typeUrl: 'enterprise/pur/admin/barterGoodsRecords',
          addUrl: 'enterprise/pur/admin/barterGoodsRecords/add',
          listUrl: 'enterprise/pur/admin/barterGoodsRecords/list',
          editUrl: 'enterprise/pur/admin/barterGoodsRecords/update',
          detailUrl: 'enterprise/pur/admin/barterGoodsRecords/detail/',
          auditUrl: 'enterprise/pur/admin/barterGoodsRecords/audit/',
          addLogisticsInfo: 'enterprise/pur/admin/barterGoodsRecords/addLogisticsInfo',
        };
      } else {
        typeObj = {
          typeNameData: '退货',
          selectTypeSrc: 'enterprise/purchase/admin/returnGoodsType/list',
          typeUrl: 'enterprise/pur/admin/returnGoodsRecords',
          addUrl: 'enterprise/pur/admin/returnGoodsRecords/add',
          listUrl: 'enterprise/pur/admin/returnGoodsRecords/list',
          editUrl: 'enterprise/pur/admin/returnGoodsRecords/update',
          detailUrl: 'enterprise/pur/admin/returnGoodsRecords/detail/',
          auditUrl: 'enterprise/pur/admin/returnGoodsRecords/audit/',
          addLogisticsInfo: 'enterprise/pur/admin/returnGoodsRecords/addLogisticsInfo',
        };
      }
    });
    $(this).removeAttr('lay-key'); //时间框一点就消失处理
    // laytpl(searchContent.innerHTML).render(htmlObj, function (html) {
    //   $('#searchDIV').html(html);
    //   laydate.render({
    //     elem: '#pTime',
    //     type: 'date',
    //     range: true,
    //     done: function (value, date, endDate) {
    //       if (value) {
    //         var dateValue = value.split(' - ');
    //         $('#sTime').val(dateValue[0] + ' 00:00:00');
    //         $('#eTime').val(dateValue[1] + ' 23:59:59');
    //       } else {
    //         $('#sTime').val();
    //         $('#eTime').val();
    //       }
    //     },
    //   });
    admin.initDataSelect('enterprise/purchase/admin/returnGoodsType/list', {}, 'typeSearch', null, typeObj.typeNameData + '类型');
    admin.initDataSelect('enterprise/purchase/admin/supplierInfoPass/list', {}, 'supplierIdSearch', null, '供应商名称', 'corporateName');

    //   form.render('select');
    // });
    table.render({
      elem: '#tableDiv',
      url: config.base + typeObj.listUrl,
      page: true,
      cols: [
        [
          {
            field: 'code',
            title: `${typeObj.typeNameData}记录编号`,
            fixed: 'left',
            width: 150,
          },
          // {
          //   field: 'typeNameData',
          //   title: `${typeObj.typeNameData}类型`,
          // },
          // {
          //   field: 'waybillNumber',
          //   title: `${typeObj.typeNameData}运单号`,
          //   width: 150,
          // },
          {
            field: 'purOrderCode',
            title: '关联采购订单编号',
            width: 150,
          },
          {
            field: 'supplierName',
            title: '供应商名称',
          },
          {
            field: 'remark',
            title: `${typeObj.typeNameData}备注`,
          },
          {
            field: 'createTime',
            title: '提交时间',
            sort: true,
          },
          {
            field: 'creatorName',
            title: `${typeObj.typeNameData}人`,
          },
          {
            field: 'totalMoney',
            title: `${typeObj.typeNameData}金额`,
            sort: true,
          },
          {
            field: 'statusName',
            title: '状态',
            width: 80,
          },
          {
            align: 'center',
            toolbar: '#returnTableBar',
            // toolbar: typeObj.typeNameData == '退货' ? '#returnTableBar' : '#barterTableBar',
            title: '操作',
            fixed: 'right',
            width: 440,
          },
        ],
      ],
    });
    $('#contractBtnAdd').click(function () {
      openForm('add');
    });
    form.on('submit(formSubSearchLog)', function (data) {
      table.reload('tableDiv', {
        where: data.field,
      });
    });
    form.on('submit(resets)', function (data) {
      $('.lay-header').find('select').val('');
      $('.lay-header').find('input').val('');
      let field = data.field;
      for (let key in field) {
        field[key] = '';
      }
      table.reload('tableDiv', {
        where: data.field,
      });
    });
    async function openFormHis(event, id) {
      const titles = {
        sendHis: '发货记录',
        getHis: '收货记录',
      };
      if (id) {
        res = await admin.reqPro(typeObj.detailUrl + id, {});
        admin.formOpen({
          area: ['1200px', '88%'],
          offset: '65px',
          title: titles[event],
          path: 'components/purchase/returnRecord/returnRecordsFormHis.html',
          success: function () {
            parentData.data = id ? res.data : {};
            parentData.eventName = event;
            parentData = { ...parentData, ...typeObj };
          },
        });
      }
    }
    async function openForm(event, id) {
      const titles = {
        detail: `查看${typeObj.typeNameData}明细`,
        edit: `编辑${typeObj.typeNameData}明细`,
        sendFun: '换货发货详情',
        getFun: '供应商发货详情',
      };
      let res;
      if (id) {
        res = await admin.reqPro(typeObj.detailUrl + id, {});
      }
      admin.formOpen({
        area: ['1200px', '88%'],
        offset: '65px',
        title: titles[event],
        path: 'components/purchase/returnRecord/returnRecordsForm.html',
        success: function () {
          parentData.data = id ? res.data : {};
          parentData.eventName = event;
          parentData = { ...parentData, ...typeObj };
        },
      });
    }

    table.on('tool(tableDiv)', function (obj) {
      const { event, data } = obj;
      if (event == 'audit') {
        layer.confirm('确定要审核吗？', { skin: 'layui-layer-admin' }, function (index) {
          admin.req(
            typeObj.auditUrl + data.id,
            {},
            function (res) {
              if (res.code === 200) {
                layer.close(index);
                layer.msg(res.msg);
                table.reload('tableDiv');
              }
            },
            'POST',
          );
          layer.close(index);
        });
      } else if (event == 'sendHis') {
        openFormHis(event, data.id);
      } else if (event == 'getHis') {
        openFormHis(event, data.id);
      } else {
        openForm(event, data.id);
      }
    });
  });
</script>
